package com.it.common.utils;

import android.text.TextUtils;

import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 脱敏工具
 * </p>
 *
 * @author ocean
 * @version 1.0.0
 * @date 2023/5/4 14:51
 */
public class EncryptUtil {

    // 手机号匹配规则
    private static final String REGEX_OF_NAME = "(?<=.{1}).";
    // 手机号替换规则
    private static final String REPLACE_OF_NAME = "*";
    // 手机号匹配规则
    private static final String REGEX_OF_PHONE = "(^\\d{3})\\d.*(\\d{4}$)";
    // 手机号替换规则
    private static final String REPLACE_OF_PHONE = "$1****$2";
    // 身份证号匹配规则
    private static final String REGEX_OF_ID_CARD = "(^\\d{3})\\d.*(\\d{4}$)";
    // 身份证号替换规则
    private static final String REPLACE_OF_ID_CARD = "$1********$2";
    // 邮箱匹配规则
    private static final String REGEX_OF_EMAIL = "(^\\S)\\S.*(@)";
    // 邮箱替换规则
    private static final String REPLACE_OF_EMAIL = "$1***$2";
    // 银行卡匹配规则
    private static final String REGEX_OF_BANK_CARD = "(^\\d{4})\\d.*(\\d{3}$)";
    // 银行卡替换规则
    private static final String REPLACE_OF_BANK_CARD = "$1********$2";

    private EncryptUtil() {
    }

    /**
     * @param 姓名
     * @return 脱敏姓名信息
     * @throws
     * @Description 姓名脱敏处理
     * @author <a href="mailto:liuhaiyang@chengxingshouhu.com">ocean</a>
     * @since 2023/5/4 14:52
     */
    public static String nameEncrypt(String name) {
        if (TextUtils.isEmpty(name) || name.length() == 1) {
            return name;
        } else if (name.length() == 2) {
            return name.substring(0, 1) + "*";
        } else {
            char[] chars = name.toCharArray();
            for (int i = 1; i < chars.length - 1; i++) {
                chars[i] = '*';
            }
            return new String(chars);
        }
    }

    /**
     * @param mobile 手机号
     * @return 脱敏手机号信息
     * @throws
     * @Description 手机号脱敏处理
     * @author <a href="mailto:liuhaiyang@chengxingshouhu.com">ocean</a>
     * @since 2023/5/4 14:52
     */
    public static String mobileEncrypt(String mobile) {
        if (StringUtils.isEmpty(mobile) || (mobile.length() != 11)) {
            return mobile;
        }
        return mobile.replaceAll(REGEX_OF_PHONE, REPLACE_OF_PHONE);
    }

    /**
     * @param idCard 身份证号
     * @return 脱敏身份证号号信息
     * @throws
     * @Description 身份证号脱敏处理
     * @author <a href="mailto:liuhaiyang@chengxingshouhu.com">ocean</a>
     * @since 2023/5/4 14:52
     */
    public static String idEncrypt(String idCard) {
        if (StringUtils.isEmpty(idCard) || (idCard.length() != 18)) {
            return idCard;
        }
        return idCard.replaceAll(REGEX_OF_ID_CARD, REPLACE_OF_ID_CARD);
    }


    /**
     * @param mail 邮箱
     * @return 脱敏邮箱号信息
     * @throws
     * @Description 邮箱脱敏处理
     * @author <a href="mailto:liuhaiyang@chengxingshouhu.com">ocean</a>
     * @since 2023/5/4 14:52
     */
    public static String mailEncrypt(String mail) {
        if (StringUtils.isEmpty(mail)) {
            return mail;
        }
        return mail.replaceAll(REGEX_OF_EMAIL, REPLACE_OF_EMAIL);
    }

    /**
     * @param bankCard 银行卡
     * @return 脱敏银行卡信息
     * @throws
     * @Description 银行卡敏处理
     * @author <a href="mailto:liuhaiyang@chengxingshouhu.com">ocean</a>
     * @since 2023/5/4 14:52
     */
    public static String bankCardEncrypt(String bankCard) {
        if (StringUtils.isEmpty(bankCard)) {
            return bankCard;
        }
        return bankCard.replaceAll(REGEX_OF_BANK_CARD, REPLACE_OF_BANK_CARD);
    }

}